#ifndef FITTING_H
#define FITTING_H
#include "../src/headfile.h"
// 使用 Eigen 库进行最小二乘法拟合
template <typename T>
void fitCircleToPoints(const std::vector<double> &x, const std::vector<double> &y, T &centerX, T &centerY, T &radius)
{
    int n = x.size();
    Eigen::MatrixXd A(n, 3);
    Eigen::VectorXd b(n);

    for (int i = 0; i < n; ++i)
    {
        A(i, 0) = 2 * x[i];
        A(i, 1) = 2 * y[i];
        A(i, 2) = -1;
        b(i) = x[i] * x[i] + y[i] * y[i];
    }

    Eigen::Vector3d sol = A.colPivHouseholderQr().solve(b);

    centerX = sol(0);
    centerY = sol(1);
    radius = std::sqrt(sol(0) * sol(0) + sol(1) * sol(1) - sol(2));
}
#endif